package org.oneblog.data.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

public class DateFormatUtil {


    public static long dateToLong(Date d, long defaultValue) {
        if (d == null) {
            return defaultValue;
        }
        return d.getTime();
    }

    public static long getSimpleDayAsLong(int hours) {
        try {
            String f = new DateTime().plusHours(hours).toString(StaticValue.DateFormatString.SIMPLE_ON_HOUR_END_HOUR);
            return Long.parseLong(f);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 
     * 返回日期的秒格式；
     * 如果当前时间是：2016-11-03 11:59:12，则返回20161103115912
     * @param hours
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static long getSimpleDaySecondsAsLong(int hours) {
        try {
            String f = new DateTime().plusHours(hours).toString(StaticValue.DateFormatString.SIMPLE_ON_SECOND_END_SECOND);
            return Long.parseLong(f);
        } catch (Exception e) {
            return 0;
        }
    }

    public static String formatLong(Long millionSeconds) {
        if(millionSeconds == null) {
            return "";
        }
        Date date = new Date(millionSeconds);
        return StaticValue.DateFormat.FULL_ON_SECOND_END_SECOND.format(date);
    }

    public static String formatCurrent() {
        return StaticValue.DateFormat.FULL_ON_SECOND_END_SECOND.format(new Date());
    }

    public static String format(Date d, Date dft) {
        try {
            return StaticValue.DateFormat.FULL_ON_SECOND_END_SECOND.format(d);
        } catch (Exception e) {
            return StaticValue.DateFormat.FULL_ON_SECOND_END_SECOND.format(dft);
        }
    }
    public static String formatMinutes(Date d) {
        try {
            return StaticValue.DateFormat.FULL_ON_MINUTE_END_MINUTE.format(d);
        } catch (Exception e) {
            return StaticValue.DateFormat.FULL_ON_MINUTE_END_MINUTE.format(new Date());
        }
    }
    public static String formatOnDay(Date d) {
        try {
            return StaticValue.DateFormat.FULL_ON_DAY_END_DAY.format(d);
        } catch (Exception e) {
            return StaticValue.DateFormat.FULL_ON_DAY_END_DAY.format(new Date());
        }
    }

    public static String formatCurrentSimple(int days) {
        try {
            return StaticValue.DateFormat.FULL_ON_DAY_END_SECOND.format(new DateTime().plusDays(days).toDate());
        } catch (Exception e) {
            return StaticValue.DateFormat.FULL_ON_DAY_END_SECOND.format(new Date());
        }
    }

    public static long formateStringToLong(String fmt) {
        return formateStringToLong(fmt, -1);
    }
    public static long formateStringToLong(String fmt, long dft) {
        try {
            if (fmt != null) {
                if (fmt.length() > 19) {
                    fmt = fmt.substring(0, 19);
                }
            }
            return DateTimeFormat.forPattern(StaticValue.DateFormatString.FULL_ON_SECOND_END_SECOND).parseDateTime(fmt).getMillis();
        } catch (Exception e) {
            return dft;
        }
    }

    public static String getFormatString(Date date, String format) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }

    public static String getDayTimeFormatString(Date date) {
        if (date == null) {
            return null;
        }
        try {
            return StaticValue.DateFormat.SIMPLE_ON_DAY_END_DAY.format(date);
        } catch (Exception e) {

        }
        return null;
    }

    public static Date formatStringToDate(String fmt) {
        try {
            return new Date(formateStringToLong(fmt));
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getDayTime(Date date) {
        if (date == null) {
            return new Date(0);
        }
        try {
            return StaticValue.DateFormat.SIMPLE_ON_DAY_END_DAY.
                    parse(StaticValue.DateFormat.SIMPLE_ON_DAY_END_DAY.format(date));
        } catch (Exception e) {

        }
        return new Date(0);
    }

    public static long offsetDateLong(int h, int minuts, int s) {
        try {
            return new DateTime().plusHours(h).plusMinutes(minuts).plusSeconds(s).getMillis();
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }

    public static long offsetDateLong(int days) {
        try {
            return new DateTime().plusDays(days).getMillis();
        } catch (Exception e) {
            return System.currentTimeMillis() + days * 86400000L;
        }
    }

    public static long offsetDateLong(Date date, int days) {
        try {
            return new DateTime(date).plusDays(days).getMillis();
        } catch (Exception e) {
            return System.currentTimeMillis() + days * 86400000L;
        }
    }

    public static long offsetDateLong(long ms, int h, int minuts, int s) {
        try {
            return new DateTime(ms).plusHours(h).plusMinutes(minuts).plusSeconds(s).getMillis();
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }

    public static long deltDayZeroTime(int day) {
        try {
            DateTime date = new DateTime().plusDays(day);

            date = date.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfDay(0);

            return date.getMillis();
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }

    public static int getSimpleDayPlusHours(int hours) {
        return getSimpleDayWithDateTime(new DateTime().plusHours(hours));
    }

    public static int getSimpleDayWithDate(Date date) {
        return getSimpleDayWithDateTime(new DateTime(date));
    }

    public static int getSimpleDayWithDateTime(DateTime date) {
        try {
            String s = date.toString(StaticValue.DateFormatString.SIMPLE_ON_DAY_END_DAY);

            int v = Integer.parseInt(s);

            return v;
        } catch (Exception e) {
            e.printStackTrace();
            return (int) (System.currentTimeMillis() / (86400 * 3600 * 24));
        }
    }

    public static int ageFromStarYear(Integer starYear, int defaultValue) {
        if (starYear != null && starYear > 0) {
            return Calendar.getInstance().get(Calendar.YEAR) - starYear + 1;
        }

        return defaultValue;
    }

    public static int ageFromTimeStamp(long birthday) {
        int now = Calendar.getInstance().get(Calendar.YEAR);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(birthday);
        return now - c.get(Calendar.YEAR) + 1;
    }
    

    public static Date formatDayBegin(Date d) {
        try {
            return new DateTime(d).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).toDate();
        } catch (Exception e) {
            return new DateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).toDate();
        }
    }

    public static Date formatDayEnd(Date d) {
        try {
            return new DateTime(d).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).toDate();
        } catch (Exception e) {
            return new DateTime().withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).toDate();
        }
    }

    public static long formateStringToLong(String fmtValue, String fmtString) {
        try {
            if (fmtValue != null) {
                if (fmtValue.length() > 19) {
                    fmtValue = fmtValue.substring(0, 19);
                }
            }
            return DateTimeFormat.forPattern(fmtString).parseDateTime(fmtValue).getMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return System.currentTimeMillis();
        }
    }

    public static void main(String[] args) {
        // System.out.println(formatLong(offsetDateLong(-25, 0, 0)));
        System.out.println(formatLong(1436941816000L));
        System.out.println(formatLong(deltDayZeroTime(-7)));

        System.out.println(getSimpleDayPlusHours(-1));

        System.out.println(ageFromStarYear(2001, 0));

        System.out.println(getDayTime(new Date(1483932358760L)));
        System.out.println(getDayTime(new Date(1483932232771L)));
        
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        System.out.println(11 / 3 + 1);
    }
}
